﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
using Microsoft.Office.Core;

namespace CopyPrint
{
    public partial class ThisAddIn
    {
        Dictionary<MailItem, bool> DicOfNewMailItems = null;
        MailItem CurrentMailItem = null;
        Inspectors inspectors = null;
        bool binIsAttachment = false;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.ItemSend += new ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            inspectors = this.Application.Inspectors;
            inspectors.NewInspector += new InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);
            DicOfNewMailItems = new Dictionary<MailItem, bool>();
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            if (DicOfNewMailItems != null)
            {
                MailItem CurrentMail = Item as MailItem;
                if (DicOfNewMailItems[CurrentMail])
                {
                    if (CurrentMail.Attachments.Count == 0)
                    {
                        if (DialogResult.No == MessageBox.Show("Do you want to Continue without an attachment?", "Outlook",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                            Cancel = true;
                    }
                }
            }
        }

        void inspectors_NewInspector(Inspector Inspector)
        {
            CurrentMailItem = Inspector.CurrentItem as MailItem;
            if (CurrentMailItem != null)
            {
                if (CurrentMailItem.EntryID == null)
                {
                    if (DialogResult.Yes == MessageBox.Show("Do you want to Create a Mail with Attachment?", "Outlook",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        binIsAttachment = true;
                    }
                    else
                    {
                        binIsAttachment = false;
                    }
                    DicOfNewMailItems.Add(CurrentMailItem, binIsAttachment);
                }
            }
        }


        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }


        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
